import { Controller } from "../../mvc/core/Controller";
import { NavbarIconInfo } from "../view/mainUI/navbar/NavbarIconInfo";
import { NavbarButtonManager } from "../view/mainUI/navbar/NavbarButtonManager";
import { ParamsConst } from "../../common/global/ParamsConst";
import { NavbarUIGuideType } from "../view/mainUI/navbar/NavbarUIGuideType";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { DataEvent } from "../events/DataEvent";
import { EventName } from "../mvc/EventName";
import { MainUITipsWindow } from "../view/mainUI/MainUITipsWindow";
import { GameController } from "../mvc/GameController";
import { ClassTypesUtil } from "../view/common/ClassTypesUtil";
import { SystemSetter } from "../view/system/SystemSetter";
import { Game } from "../Game";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { LevelLimitDefin } from "../../common/defin/LevelLimitDefin";
import { NavbarButtonUIRow } from "../view/mainUI/navbar/NavbarButtonUIRow";
import { MainUINavbarConfig } from "../resource/MainUINavbarConfig";
import { ModuleType } from "../view/common/ModuleType";
import { CallLater } from "../../common/net/CallLater";
type int = number;
//class MainUINavbarController
    
    export  class MainUINavbarController extends Controller
    {
        constructor()
        {
            
            super();this._opList = [];
            this.Row1BtnNames = cache.mainUINavbar.Row1BtnNames;
            this.Row2BtnNames = cache.mainUINavbar.Row2BtnNames;
            return;
        }

        /* internal  */setIconShowingList(arg1: Array<any>, arg2: Array<any>, arg3: NavbarIconInfo): void
        {
            var loc1;
            if ((loc1 = arg2.indexOf(arg3.iconName)) < 0) 
            {
                return;
            }
            arg1[loc1] = true;
            return;
        }

        /* internal  */get uiManager(): NavbarButtonManager
        {
            if (this._uiManager == null) 
            {
                this._uiManager = new NavbarButtonManager();
            }
            return this._uiManager;
        }

        /* internal  */isCanFairlyRandShow(): Boolean
        {
            var loc1=ParamsConst.instance.fairylandLevel > cache.role.entityInfo.level;
            return !(cache.copy.isFairylandOpen || loc1);
        }

        public takeAction(): void
        {
            this.parseForSave();
            if (this._isOperating) 
            {
                return;
            }
            var loc1=this._opList.shift();
            if (loc1 == null) 
            {
                this._isOperating = false;
                return;
            }
            this._isOperating = true;
            this._curOpInfo = loc1;
            var loc2=loc1.guideType;
            switch (loc2) 
            {
                case NavbarUIGuideType.HideTaskInfoPanel:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.NpcDialogTaskInfoPanelShowHide, false));
                    this.opFinishedHandler(null);
                    break;
                }
                case NavbarUIGuideType.ShowTaskInfoPanel:
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.NpcDialogTaskInfoPanelShowHide, true));
                    this.opFinishedHandler(null);
                    break;
                }
                case NavbarUIGuideType.ItemDropEffect:
                {
                    this.uiManager.showItemDropToBtn(loc1);
                    break;
                }
                case NavbarUIGuideType.ShowMoutGuide: case NavbarUIGuideType.ShowGuidePanel:
                {
                    MainUITipsWindow.instace.showGuidPanel(loc1);
                    break;
                }
                case NavbarUIGuideType.ShowIcon:
                {
                    this.uiManager.flyAndShowBtn(loc1);
                    break;
                }
                case NavbarUIGuideType.ShowPetChuZhan:
                {
                    GameController.pet.view.show();
                    GameController.pet.view.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onPetViewCloseHandler);
                    break;
                }
                case NavbarUIGuideType.DispatchEvent:
                {
                    Dispatcher.dispatchEvent(new DataEvent(loc1.guideDesc, {}));
                    this.opFinishedHandler(null);
                    break;
                }
                default:
                {
                    this._curOpInfo = null;
                    this._isOperating = false;
                    break;
                }
            }
            return;
        }

        /* internal  */parseForSave(): void
        {
            var loc2=null;
            var loc3=0;
            var loc4=0;
            if (this._opList == null || this._opList.length == 0) 
            {
                return;
            }
            var loc1=0;
            while (loc1 < this._opList.length) 
            {
                loc2 = this._opList[loc1] as NavbarIconInfo;
                if (loc2.guideType == NavbarUIGuideType.ShowIcon) 
                {
                    loc3 = this.getIconIndex(loc2);
                    if (loc3 > 0 && ClassTypesUtil.getBitValue(SystemSetter.localSetter.navbarIconShowed, loc3) == 0) 
                    {
                        loc4 = ClassTypesUtil.getSetBitValueResult(SystemSetter.localSetter.navbarIconShowed, loc3, 1);
                        SystemSetter.localSetter.navbarIconShowed = loc4;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.Sys_SaveInfoNotShowInWindow));
                    }
                }
                ++loc1;
            }
            return;
        }

        /* internal  */onPetViewCloseHandler(arg1: Object /* flash.events.Event */): void
        {
            GameController.pet.view.removeEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onPetViewCloseHandler);
            this.opFinishedHandler(null);
            return;
        }

        /* internal  */getIconIndex(arg1: NavbarIconInfo): int
        {
            var loc1=0;
            if (arg1.iconRow == 1) 
            {
                return this.Row1BtnNames.indexOf(arg1.iconName);
            }
            if (arg1.iconRow == 2) 
            {
                loc1 = this.Row2BtnNames.indexOf(arg1.iconName);
                if (loc1 < 0) 
                {
                    return -1;
                }
                return this.Row1BtnNames.length + loc1;
            }
            return -1;
        }

        protected /* override */ initServer(): void
        {
            if (!cache.mainUINavbar.isUIByLevel) 
            {
                return;
            }
            this._isSceneInited = false;
            this._isSysSettingGot = false;
            Dispatcher.addEventListener(EventName.SysSettingSuccess, this.onGotSysSetting);
            if (Game.isSceneInit()) 
            {
                this.onSceneUpdated(null);
            }
            else 
            {
                Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdated);
            }
            return;
        }

        /* internal  */onSceneUpdated(arg1: DataEvent): void
        {
            this._isSceneInited = true;
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onSceneUpdated);
            this.tryInitControlUI();
            return;
        }

        /* internal  */onGotSysSetting(arg1: DataEvent): void
        {
            this._isSysSettingGot = true;
            Dispatcher.removeEventListener(EventName.SysSettingSuccess, this.onGotSysSetting);
            this.tryInitControlUI();
            return;
        }

        /* internal  */tryInitControlUI(): void
        {
            if (!(this._isSceneInited && this._isSysSettingGot) || !cache.mainUINavbar.isUIByLevel) 
            {
                return;
            }
            this.uiManager.init(GameController.gameui.getGameui().navbar, this.Row1BtnNames, GameController.gameui.getGameui().navbarSprite, this.Row2BtnNames);
            this.refleshUIRows();
            this.checkFairyLandAndExpolerShop();
            if (this.isLevelNeedEffect) 
            {
                NetDispatcher.addCmdListener(ServerCommand.TaskGetRes, this.onTaskGetRes);
                NetDispatcher.addCmdListener(ServerCommand.TaskEndRes, this.onTaskEndRes);
                Dispatcher.addEventListener(EventName.MainUIByLevelMoutGetXueHu, this.onGetOnlineAwardHandler);
            }
            if (cache.role.roleInfo.level < LevelLimitDefin.WuxingShowLevel) 
            {
                Dispatcher.addEventListener(EventName.MainUIByLevelOpFinish, this.opFinishedHandler);
                NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            }
            Dispatcher.addEventListener(EventName.MainUIByLevelShowArrow, this.onAskShowArrowHandler);
            NetDispatcher.addCmdListener(ServerCommand.UIByLevelFairyOpenedChange, this.fairyLandOpenChangeHandler);
            return;
        }

        /* internal  */refleshUIRows(): void
        {
            var loc1=SystemSetter.localSetter.navbarIconShowed;
            this.refreshUIRow(this.uiManager.row1, this.Row1BtnNames, 0, (this.Row1BtnNames.length - 1));
            this.refreshUIRow(this.uiManager.row2, this.Row2BtnNames, this.Row1BtnNames.length, (this.Row1BtnNames.length + this.Row2BtnNames.length - 1));
            if (loc1 != SystemSetter.localSetter.navbarIconShowed) 
            {
                cache.mainUINavbar.saveShowingStatusValue();
            }
            return;
        }

        /* internal  */refreshUIRow(arg1: NavbarButtonUIRow, arg2: Array<any>, arg3: int, arg4: int): void
        {
            var loc3=null;
            var loc1=cache.mainUINavbar.getShowedStatus(arg3, arg4);
            var loc2=MainUINavbarConfig.instance.data;
            var loc4=0;
            var loc5=loc2;
            for(loc3 of loc5) 
            {
                if (!(loc3.guideType == NavbarUIGuideType.ShowIcon) || loc3.showLevel > cache.role.roleInfo.level) 
                {
                    continue;
                }
                this.setIconShowingList(loc1, arg2, loc3);
            }
            arg1.initShow(loc1);
            cache.mainUINavbar.updateShowingListRecord(loc1, arg3);
            return;
        }

        /* internal  */onTaskGetRes(arg1: Object): void
        {
            if (!this.isLevelNeedEffect) 
            {
                NetDispatcher.removeCmdListener(ServerCommand.TaskGetRes, this.onTaskGetRes);
                return;
            }
            var loc1=this.getIconInfosByTaskInfo(arg1, NavbarUIGuideType.GetTask);
            if (loc1 == null || loc1.length == 0) 
            {
                return;
            }
            var loc2=loc1[0] as NavbarIconInfo;
            if (!(loc2.playerLevel == 0) && loc2.playerLevel < cache.role.roleInfo.level) 
            {
                return;
            }
            this.addAndAllNextsToOpList(loc2);
            this.takeAction();
            return;
        }

        /* internal  */onTaskEndRes(arg1: Object): void
        {
            if (!this.isLevelNeedEffect) 
            {
                NetDispatcher.removeCmdListener(ServerCommand.TaskEndRes, this.onTaskEndRes);
                return;
            }
            var loc1=this.getIconInfosByTaskInfo(arg1, NavbarUIGuideType.FinishTask);
            if (loc1 == null || loc1.length == 0) 
            {
                return;
            }
            var loc2=loc1[0] as NavbarIconInfo;
            if (!(loc2.playerLevel == 0) && loc2.playerLevel < cache.role.roleInfo.level) 
            {
                return;
            }
            this.addAndAllNextsToOpList(loc2);
            this.takeAction();
            return;
        }

        /* internal  */onRoleLevelUpdateHandler(arg1: Object): void
        {
            if (!this.isLevelNeedEffect) 
            {
                if (cache.role.roleInfo.level == LevelLimitDefin.WuxingShowLevel) 
                {
                    this.checkLevelForShowIcon();
                    NetDispatcher.removeCmdListener(ServerCommand.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
                    return;
                }
                this.refleshUIRows();
                this.checkFairyLandAndExpolerShop();
                return;
            }
            this.checkFairyLandAndExpolerShop();
            this.checkLevelUpSteps();
            this.checkLevelForShowIcon();
            return;
        }

        /* internal  */get isLevelNeedEffect(): Boolean
        {
            return cache.role.roleInfo.level < LevelLimitDefin.UIByLevelShowIconNoEffectLevel;
        }

        /* internal  */checkFairyLandAndExpolerShop(): void
        {
            var loc1=ParamsConst.instance.exploreShopLevel > cache.role.entityInfo.level;
            var loc2=cache.role.isHideFairyland;
            var loc3=!loc2 || loc1;
            this.showHideShop(ModuleType.Fairyland, loc2);
            this.showHideShop(ModuleType.ExploreShop, loc3);
            return;
        }

        /* internal  */showHideShop(arg1: string, arg2: Boolean): void
        {
            var loc3=null;
            var loc1=this.Row1BtnNames.indexOf(arg1);
            if (loc1 < 0) 
            {
                return;
            }
            var loc2=this.uiManager.row1.showStatusList;
            if (arg2) 
            {
                if (ClassTypesUtil.getBitValue(SystemSetter.localSetter.navbarIconShowed, loc1) == 1) 
                {
                    loc2[loc1] = false;
                    this.uiManager.row1.initShow(loc2);
                }
            }
            else if (this.isLevelNeedEffect) 
            {
                (loc3 = new NavbarIconInfo()).guideType = NavbarUIGuideType.ShowIcon;
                loc3.iconName = arg1;
                loc3.iconRow = 1;
                this.addInfoToOpList(loc3);
                this.takeAction();
            }
            else 
            {
                loc2[loc1] = true;
                this.uiManager.row1.initShow(loc2);
            }
            return;
        }

        /* internal  */checkLevelForShowIcon(): void
        {
            var loc1=null;
            var loc2=MainUINavbarConfig.instance.data;
            var loc3=0;
            var loc4=loc2;
            for(loc1 of loc4) 
            {
                if (!(loc1.guideType == NavbarUIGuideType.ShowIcon && loc1.showLevel <= cache.role.roleInfo.level)) 
                {
                    continue;
                }
                this.addInfoToOpList(loc1);
                this.takeAction();
            }
            return;
        }

        /* internal  */checkLevelUpSteps(): void
        {
            var loc1=null;
            var loc3=0;
            var loc2=MainUINavbarConfig.instance.getInfoByJudge(["playerLevel"], [cache.role.roleInfo.level]);
            if (!(loc2 == null) && loc2.length > 0) 
            {
                loc3 = 0;
                while (loc3 < loc2.length) 
                {
                    loc1 = loc2[loc3] as NavbarIconInfo;
                    if (loc1.condition == NavbarUIGuideType.Condition_LevelUP) 
                    {
                        this.addAndAllNextsToOpList(loc1);
                        this.takeAction();
                    }
                    ++loc3;
                }
            }
            return;
        }

        /* internal  */addAndAllNextsToOpList(arg1: NavbarIconInfo): void
        {
            this.addInfoToOpList(arg1);
            var loc1=arg1;
            while (loc1.nextId > 0) 
            {
                loc1 = MainUINavbarConfig.instance.getInfoById(loc1.nextId);
                if (loc1 == null) 
                {
                    break;
                }
                this.addInfoToOpList(loc1);
            }
            return;
        }

        /* internal  */addInfoToOpList(arg1: NavbarIconInfo): void
        {
            if (this._opList.indexOf(arg1) >= 0) 
            {
                return;
            }
            this._opList.push(arg1);
            return;
        }

        /* internal  */opFinishedHandler(arg1: DataEvent): void
        {
            CallLater.addCallBack(this.callNextFrame);
            return;
        }

        /* internal  */callNextFrame(): void
        {
            this._isOperating = false;
            this._curOpInfo = null;
            this.takeAction();
            return;
        }

        /* internal  */onGetOnlineAwardHandler(arg1: DataEvent): void
        {
            if (!this.isLevelNeedEffect) 
            {
                Dispatcher.removeEventListener(EventName.MainUIByLevelMoutGetXueHu, this.onGetOnlineAwardHandler);
            }
            var loc1=MainUINavbarConfig.instance.getInfoByJudge(["guideType", "playerLevel"], [NavbarUIGuideType.ShowMoutGuide, cache.role.roleInfo.level])[0] as NavbarIconInfo;
            if (loc1 == null) 
            {
                return;
            }
            this.addAndAllNextsToOpList(loc1);
            this.takeAction();
            return;
        }

        /* internal  */fairyLandOpenChangeHandler(arg1: Object): void
        {
            this.checkFairyLandAndExpolerShop();
            return;
        }

        /* internal  */onAskShowArrowHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=String(arg1.data);
            var loc3;
            if ((loc3 = this.Row1BtnNames.indexOf(loc1)) >= 0) 
            {
                loc2 = this.uiManager.row1;
            }
            else if (this.Row2BtnNames.indexOf(loc1) >= 0) 
            {
                loc3 = this.Row2BtnNames.indexOf(loc1);
                loc2 = this.uiManager.row2;
            }
            if (loc2 == null || loc3 < 0) 
            {
                return;
            }
            loc2.showGuideArrow(loc3);
            return;
        }

        /* internal  */getIconInfosByTaskInfo(arg1: Object, arg2: int): Array<any>
        {
            var loc1=[];
            if (arg1 == null) 
            {
                return loc1;
            }
            if (!arg1.tasks || !arg1.task) 
            {
                return loc1;
            }
            var loc2;
            var loc3=(loc2 = arg1.task).sTask.code;
            var loc4=[arg2, loc3.toString()];
            loc1 = MainUINavbarConfig.instance.getInfoByJudge(["taskType", "taskCodes"], loc4);
            return loc1;
        }

        private /* var */Row1BtnNames: Array<any>;

        private /* var */Row2BtnNames: Array<any>;

        private /* var */_opList: Array<any>;

        private /* var */_isOperating: Boolean=false;

        private /* var */_curOpInfo: NavbarIconInfo;

        private /* var */_isSceneInited: Boolean;

        private /* var */_isSysSettingGot: Boolean;

        private /* var */_uiManager: NavbarButtonManager;
    }
